package chances.epg.navigator.service;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import chances.epg.data.service.BizService;
import chances.epg.navigator.template.loader.DBTemplateConfigLoader;
import chances.epg.navigator.template.loader.TemplateConfigLoader;
import chances.epg.navigator.template.loader.XMLTemplateConfigLoader;

@Component
public class TemplateConfigLoaderFactory implements FactoryBean<TemplateConfigLoader> {

	@Value("${epg.template.load:db}")
	private String loadType;

	@Autowired
	private BizService bizService;

	@Value("${epg.template.bind.xml:'/WEB-INF/bind.xml'}")
	private Resource xmlResource;

	@Override
	public TemplateConfigLoader getObject() throws Exception {
		if ("db".equals(loadType)) {
			return new DBTemplateConfigLoader(bizService);
		} else if ("xml".equals(loadType)) {
			XMLTemplateConfigLoader loader = new XMLTemplateConfigLoader();
			loader.setConfigs(new String[] { xmlResource.getFile().toString() });
			return loader;
		} else {
			throw new Exception("tempload " + this.loadType + "error");
		}
	}

	@Override
	public Class<?> getObjectType() {

		return TemplateConfigLoader.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
